package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.enums.UnitStatus;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.SysBuildingUnitMapper;
import com.ruoyi.system.mapper.SysTenantMapper;
import com.ruoyi.system.mapper.SysUnitTenantMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysUnitMapper;
import com.ruoyi.system.service.ISysUnitService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;
import sun.management.counter.Units;

/**
 * 单元信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-06-24
 */
@Service
public class SysUnitServiceImpl implements ISysUnitService 
{
    @Autowired
    private SysUnitMapper sysUnitMapper;

    @Autowired
    private SysBuildingUnitMapper sysBuildingUnitMapper;

    @Autowired
    private SysTenantMapper sysTenantMapper;

    @Autowired
    private SysUnitTenantMapper sysUnitTenantMapper;

    /**
     * 查询单元信息
     * 
     * @param unitId 单元信息ID
     * @return 单元信息
     */
    @Override
    public SysUnit selectSysUnitById(Long unitId)
    {
        return sysUnitMapper.selectSysUnitById(unitId);
    }

    /**
     * 查询单元信息
     *
     * @param unitIds 单元信息ID
     * @return 单元信息
     */
    @Override
    public List<SysUnit> selectSysUnitByIds(String unitIds) {
        return sysUnitMapper.selectSysUnitByIds(Convert.toStrArray(unitIds));
    }

    /**
     * 查询单元信息列表
     * 
     * @param sysUnit 单元信息
     * @return 单元信息
     */
    @Override
    public List<SysUnit> selectSysUnitList(SysUnit sysUnit)
    {
        return sysUnitMapper.selectSysUnitList(sysUnit);
    }

    @Override
    public List<SysUnit> selectSysUnitListAreadly(SysUnit sysUnit) {
        return sysUnitMapper.selectSysUnitListAreadly(sysUnit);
    }

    /**
     * 查询所有单元信息列表
     *
     * @return 单元信息
     */
    @Override
    public List<SysUnit> selectSysUnitAll()
    {
        return sysUnitMapper.selectSysUnitAll();
    }

    /**
     * 根据楼栋id查询所有单元信息
     *@param buildingId 楼栋id
     * @return 单元信息集合
     */
    @Override
    public List<SysUnit> selectSysUnitByBuildingId(Long buildingId) {
        if(buildingId!=null) {
            return sysUnitMapper.selectSysUnitByBuildingId(buildingId);
        }else {
            return null;
        }

    }

    /**
     * 根据租户id查询所有单元信息
     *@param tenantId 租户id
     * @return 单元信息集合
     */
    @Override
    public List<SysUnit> selectSysUnitByTenantId(Long tenantId,Long buildingId) {
        SysTenant sysTenant = sysTenantMapper.selectSysTenantById(tenantId);
        List<SysUnit> unitList = sysUnitMapper.selectSysUnitByBuildingId(buildingId);
        for (SysUnit sysUnit : unitList) {
            if (sysTenant.getUnitIdStr().contains(sysUnit.getUnitId().toString())) {
                sysUnit.setFlag(true);
            }
        }
        return unitList;
    }

    /**
     * 查询统计数据
     * @return 单元信息统计集合
     */
    @Override
    public List<SysUnit> selectSysUnitStatList() {
        return sysUnitMapper.selectSysUnitStatList();
    }

    /**
     * 新增单元信息
     * 
     * @param sysUnit 单元信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysUnit(SysUnit sysUnit)
    {
        //判断是否已存在
        SysUnit findSysUnit = new SysUnit();
        findSysUnit.setBuildingId(sysUnit.getBuildingId());
        findSysUnit.setUnitName(sysUnit.getUnitName());
        List<SysUnit> list = sysUnitMapper.selectSysUnitList(findSysUnit);
        if(list!=null && !list.isEmpty()){
            return 0;
        }

        sysUnit.setCreateTime(DateUtils.getNowDate());
        int rows = sysUnitMapper.insertSysUnit(sysUnit);
        //新增楼栋和单元的关联
        SysBuildingUnit sysBuildingUnit = new SysBuildingUnit(sysUnit.getBuildingId(),sysUnit.getUnitId());
        sysBuildingUnitMapper.insertSysBuildingUnit(sysBuildingUnit);
        return rows;
    }

    /**
     * 修改单元信息
     * 
     * @param sysUnit 单元信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSysUnit(SysUnit sysUnit)
    {
        sysUnit.setUpdateTime(DateUtils.getNowDate());
        int rows = sysUnitMapper.updateSysUnit(sysUnit);

        return rows;
    }

    /**
     * 删除单元信息对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysUnitByIds(String ids)
    {
        //只要空闲状态才可删除
        List<SysUnit> list = sysUnitMapper.selectSysUnitByIds(Convert.toStrArray(ids));
        if(list!=null && !list.isEmpty()){
            for (SysUnit sysUnit : list) {
                if(!sysUnit.getUnitStatus().equals(UnitStatus.FREE.getCode())){
                    return 0;
                }
            }
        }
        int rows = sysUnitMapper.deleteSysUnitByIds(Convert.toStrArray(ids));
        sysBuildingUnitMapper.deleteSysBuildingUnitByUnitIds(Convert.toStrArray(ids));
        return rows;
    }

    /**
     * 删除单元信息信息
     * 
     * @param unitId 单元信息ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysUnitById(Long unitId)
    {
        SysUnit sysUnit = sysUnitMapper.selectSysUnitById(unitId);
        if(!sysUnit.getUnitStatus().equals(UnitStatus.FREE.getCode())){
            return 0;
        }
        int rows = sysUnitMapper.deleteSysUnitById(unitId);
        sysBuildingUnitMapper.deleteSysBuildingUnitByUnitId(unitId);
        return rows;
    }
}
